package com.sixkey.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixkey.annotation.OperationLog;
import com.sixkey.entity.User;
import com.sixkey.entity.vo.LoginVo;
import com.sixkey.entity.vo.UserVo;
import com.sixkey.response.Response;
import com.sixkey.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-09-27
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    @PostMapping("/login")
    public Response login(@RequestBody @Valid LoginVo loginVo) {
        Map<String,Object> map = userService.doLogin(loginVo);
        return Response.success(map);
    }

    /**
     * 用户退出
     * @param
     * @return
     */
    @PostMapping("/logout/{userId}")
    public Response logout(@PathVariable("userId") Integer userId){
        log.info("用户退出请求进来~");
        StpUtil.logout(userId);
        log.info("token: {}",StpUtil.getTokenValue());
        log.info("tokenInfo: {}",StpUtil.getTokenInfo());
        return Response.success();
    }


    /**
     * 分页获取用户数据
     * @return
     */
    @GetMapping("/list/{pageNo}/{pageSize}")
    public Response list(@PathVariable("pageNo") Integer pageNo,
                         @PathVariable("pageSize") Integer pageSize,
                         UserVo userVo) {
        log.info("分页查询用户数据请求进来");
        Page<User> userPage = userService.queryList(pageNo,pageSize,userVo);
        return Response.success(userPage);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/save")
    @OperationLog("新增用户")
    public Response save(@RequestBody @Valid User user){
        log.info("新增用户请求进来");
       userService.saveUser(user);
        return Response.success();
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping("/update")
    @OperationLog("修改用户")
    public Response update(@RequestBody @Valid User user){
        log.info("修改用户请求进来");
        userService.updateUser(user);
        return Response.success();
    }

    /**
     * 删除用户
     * @param
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @OperationLog("删除用户")
    public Response remove(@PathVariable("id") List<Integer> id){
        log.info("删除用户请求进来");
        userService.removeBatch(id);
        return Response.success();
    }

    /**
     * 修改用户状态
     * @param
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    @OperationLog("修改用户状态")
    public Response updateStatus(@PathVariable("id") Integer id,
                           @PathVariable("status") Integer status){
        log.info("修改用户状态请求进来");
        userService.updateStatus(id,status);
        return Response.success();
    }

    /**
     * 用户数据回显
     * @param
     * @return
     */
    @GetMapping("/echo/{id}")
    public Response getUserInfo(@PathVariable("id") Integer id){
        log.info("户数据回显请求进来");
        User user = userService.getUserById(id);
        return Response.success(user);
    }
}
